package com.knife.bbs.action;

import java.util.Iterator;

import com.knife.bbs.business.ActiveUser;
import com.knife.bbs.business.ActiveUserService;
import com.knife.bbs.business.BBSRights;
import com.knife.bbs.business.BBSUserService;
import com.knife.bbs.business.MessageUtil;
import com.knife.bbs.domain.Friend;
import com.knife.bbs.domain.WebMessage;
import com.knife.util.CommUtil;
import com.knife.web.ActionContext;
import com.knife.web.Module;
import com.knife.web.Page;
import com.knife.web.WebForm;
import com.knife.web.tools.IPageList;

public class FriendAction extends BBSAction {
	private BBSUserService userService = BBSUserService.getInstance();

	public Page doInit(WebForm form, Module module) {
		// TODO Auto-generated method stub
		return null;
	}

	private ActiveUser getCurrentUser() {
		ActiveUser user = (ActiveUser) ActionContext.getContext().getSession()
				.getAttribute("bbsuser");
		return user;
	}

	public Page doList(WebForm form, Module module) {
		if (!BBSRights.checkRights(new Friend(), "list", getCurrentUser()))
			return new Page("popedomError", "norights.htm", "html","/"+skin+"/");
		return doQuery(form, module);
	}

	public Page doQuery(WebForm form, Module module) {
		int currentPage = CommUtil.null2Int(form.get("page"));
		int pageSize = CommUtil.null2Int(form.get("pageSize"));
		if (currentPage < 1)
			currentPage = 1;
		if (pageSize < 1)
			pageSize = 15;
		IPageList pList = userService.queryUserFriends("1=1 and owner='"
				+ userService.getUserInfoByName(getCurrentUser().getUsername())
						.getCid() + "'", pageSize, currentPage);
		if (pList.getResult() != null) {
			for (Iterator it = pList.getResult().iterator(); it.hasNext();) {
				Friend f = (Friend) it.next();
				ActiveUser user = new ActiveUser();
				user.setUsername(f.getOwner());
				f.setState(new Integer(ActiveUserService.isOnline(user
						.getUsername()) ? "1" : "0"));

			}
		}
		if (pList != null)
			com.knife.util.CommUtil.saveIPageList2WebForm(pList, form);

		return module.findPage("list");
	}

	public Page doDel(WebForm form, Module module) {
		if (!BBSRights.checkRights(new Friend(), "list", getCurrentUser()))
			return new Page("popedomError", "norights.htm", "html","/"+skin+"/");
		String cid = CommUtil.null2String(form.get("cid"));
		if (cid != null) {
			Friend f = userService.getUserFriend(cid);
			userService.del(f);
		}
		return new Page("FriendsList", contextPath+"/bbsfriends.do?parameter=list", "url");
	}

	public Page doAdd(WebForm form, Module module) {
		if (!BBSRights.checkRights(new Friend(), "add", getCurrentUser()))
			return new Page("popedomError", "norights.htm", "html","/"+skin+"/");
		String friend = CommUtil.null2String(form.get("friend"));
		String myname=getCurrentUser().getUsername();
		String owner = userService.getUserInfoByName(myname).getCid();
		if (friend != null && owner != null && !myname.equals(friend)) {
			Friend f = new Friend();
			f.setFriend(friend);
			f.setOwner(owner);
			// f.setName(userService.getUserInfo(friend).getUsername());
			if (userService.add(f)) {
				MessageUtil.sendSystemMessage(WebMessage.FRIENDREQMSG,
						getCurrentUser().getUsername());
			}
		}
		return new Page("FriendsList", contextPath+"/bbsfriends.do?parameter=list", "url");
	}

	public Page doAddByName(WebForm form, Module module) {
		if (!BBSRights.checkRights(new Friend(), "add", getCurrentUser()))
			return new Page("popedomError", "norights.htm", "html","/"+skin+"/");
		String friendname = CommUtil.null2String(form.get("friendname"));
		String friend = "";
		if (friendname != null) {
			friend = userService.getUserInfoByName(friendname).getCid();
		}
		String owner = userService.getUserInfoByName(
				getCurrentUser().getUsername()).getCid();
		if (friend != null && owner != null) {
			Friend f = new Friend();
			f.setFriend(friend);
			f.setOwner(owner);
			// f.setName(friendname);
			if (userService.add(f)) {
				MessageUtil.sendSystemMessage(WebMessage.FRIENDREQMSG,
						getCurrentUser().getUsername());
			}
		}
		return new Page("FriendsList", contextPath+"/bbsfriends.do?parameter=list", "url");
	}

	public Page doPreAdd(WebForm form, Module module) {
		return module.findPage("/"+skin+"/","add");
	}

}
